package com.zwj.batch.config.test;

import com.zwj.batch.config.springBatch.decider.BatchConfig13Decider;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.context.annotation.Bean;

import javax.annotation.Resource;

/**
 *  条件分支控制-使用自定义状态值 DEMO
 * @author 张文接
 * @version 1.0
 * @createTime 2023/6/24 15:55
 */
//@Configuration
public class BatchConfig13 {
    @Resource
    private JobBuilderFactory jobBuilderFactory;
    @Resource
    private StepBuilderFactory stepBuilderFactory;

    @Bean
    public Tasklet taskletFirst() {
        return new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
                System.out.println("--------------taskletFirst---------------");
                return RepeatStatus.FINISHED;
            }
        };
    }

    @Bean
    public Tasklet taskletA() {
        return new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
                System.out.println("--------------taskletA---------------");
                return RepeatStatus.FINISHED;
            }
        };
    }

    @Bean
    public Tasklet taskletB() {
        return new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
                System.out.println("--------------taskletB---------------");
                return RepeatStatus.FINISHED;
            }
        };
    }

    @Bean
    public Tasklet taskletDefault() {
        return new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
                System.out.println("--------------taskletDefault---------------");
                return RepeatStatus.FINISHED;
            }
        };
    }


    @Bean
    public Step firstStep() {
        return stepBuilderFactory
                .get("firstStep")
                .tasklet(taskletFirst())
                .build();
    }

    @Bean
    public Step stepA() {
        return stepBuilderFactory
                .get("stepA")
                .tasklet(taskletA())
                .build();
    }

    @Bean
    public Step stepB() {
        return stepBuilderFactory
                .get("stepB")
                .tasklet(taskletB())
                .build();
    }

    @Bean
    public Step defaultStep() {
        return stepBuilderFactory
                .get("defaultStep")
                .tasklet(taskletDefault())
                .build();
    }

    @Bean
    public BatchConfig13Decider statusDecider() {
        return new BatchConfig13Decider();
    }

    @Bean
    public Job job() {
        return jobBuilderFactory
                .get("customize-step-job")
                .start(firstStep())
                .next(statusDecider())//加一层自定义状态
                .from(statusDecider()).on("A").to(stepA())
                .from(statusDecider()).on("B").to(stepB())
                .from(statusDecider()).on("*").to(defaultStep())
                .end()
                .incrementer(new RunIdIncrementer())
                .build();
    }
}
